package javbot.command;

import java.awt.AWTException;
import java.io.File;

import javbot.GlobalClass;
import javbot.util.CommandLineExecutor;

public class CmdExecute extends Command {

	final private String execute;
	final private String file;
	
	public CmdExecute(String execute) throws AWTException {
		this(execute,null);
	}

	public CmdExecute(String execute, String file) throws AWTException {
		super();
		
		this.file = file;
		
		if (execute.toLowerCase().startsWith("dir"))
			this.execute = "cmd /c dir" + execute.substring(3);
		else 
			this.execute = execute;
		
		
	}

	public void execute() {
		
		System.out.println("*********************** Executing 'Execute'" + file);
		
		if (file != null) {
			CommandLineExecutor.outputToFile = new File(file);
		}
		
		CommandLineExecutor.outputToSysout = true;
		CommandLineExecutor.cmdExecute(execute);
		
		if (file !=null) {
			GlobalClass.files.add(CommandLineExecutor.outputToFile);
		}
	}
	
	public String toString() {
		if (file != null)
			return "Execute '" + execute + "' >> " + file;
		else
			return "Execute '" + execute + "'";
	}	
}
